/*
* Copyright (C) 2004-2010  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "stdafx.h"
#include "FdoWcsXmlGlobals.h"
#include "FdoWcsCoverageDescriptions.h"
#include "FdoCommonOSUtil.h"
#include "OWS/FdoOwsGlobals.h"

FdoWcsCoverageDescriptions::FdoWcsCoverageDescriptions(void)
{
}

FdoWcsCoverageDescriptions::~FdoWcsCoverageDescriptions(void)
{
}

FdoWcsCoverageDescriptions* FdoWcsCoverageDescriptions::Create()
{
    return new FdoWcsCoverageDescriptions();
}

void FdoWcsCoverageDescriptions::Dispose()
{
    delete this;
}

FdoXmlSaxHandler* FdoWcsCoverageDescriptions::XmlStartElement( FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname, FdoXmlAttributeCollection* atts )
{
    FdoXmlSaxHandler* pRet = NULL;

    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        FdoOwsXmlSaxContext* myContext = static_cast<FdoOwsXmlSaxContext*>(context);
        switch (myContext->StateServiceMetadata())
        {
            // state 0 represents that we are now in document root
            // possible sub element only include <CoverageDescription>
        case 0:
            {
                if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCoverageDescriptions) == 0)
                {
                    FdoPtr<FdoXmlAttribute> attr = atts->FindItem(FdoOwsGlobals::version);
                    if (attr != NULL)
                        mVersion = attr->GetValue();

                    // TODO: update the FdoOwsXmlSaxContext to add a method to reflect the name 
                    // of coverage description.
                    myContext->SetStateServiceMetadata(1);
                }
                else
                    throw FdoException::Create(FdoException::NLSGetMessage(
                    FDO_NLSID(FDO_52_BADSUBELEMENT), 
                    "Error reading from XML, unexpected element %1$ls inside '%2$ls'.", 
                    name, FdoWcsXmlGlobals::WcsCoverageDescriptions));
                break;
            }
            // state 1 means that we are in nested level 1, 
            // possible sub element only include <CoverageDescription>
        case 1:
            {
                if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCoverageDescription) == 0)
                {
                    FdoWcsCoverageDescriptionP pCoverageDesc = FdoWcsCoverageDescription::Create();
                    this->Add(pCoverageDesc);

                    pRet = pCoverageDesc;
                }

                break;
            }
        default:
            throw FdoCommandException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_57_UNEXPECTEDERROR), "Unexpected error encountered."));
        }
    }
    catch (FdoException* e)
    {
        context->AddError(e);
        e->Release();
    }
    
    return pRet;
}

FdoBoolean FdoWcsCoverageDescriptions::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        FdoOwsXmlSaxContext* myContext = static_cast<FdoOwsXmlSaxContext*>(context);
        switch (myContext->StateServiceMetadata())
        {
        case 1:
            {
                if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCoverageDescriptions) == 0)
                    myContext->SetStateServiceMetadata(0);
                else
                    return BaseType::XmlEndElement(context, uri, name, qname);

                break;
            }
        default:
            throw FdoCommandException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_57_UNEXPECTEDERROR), "Unexpected error encountered."));
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return BaseType::XmlEndElement(context, uri, name, qname);
}

FdoXmlSaxContext* FdoWcsCoverageDescriptions::GetSaxContext()
{
    return new FdoOwsXmlSaxContext(FdoPtr<FdoXmlReader>(GetXmlReader()));
}

FdoWcsCoverageDescription* FdoWcsCoverageDescriptions::GetCoverageDescription(FdoString *identifier) const
{
    FdoInt32 count = this->GetCount();
    for (FdoInt32 i = 0; i < count; ++i)
    {
        FdoWcsCoverageDescriptionP pCoverageDesc = this->GetItem(i);
        if (FdoCommonOSUtil::wcsicmp(pCoverageDesc->GetIdentifier(), identifier) == 0)
            return FDO_SAFE_ADDREF(pCoverageDesc.p);
    }

    return NULL;
}